package com.nkdx.ws.controller;

import com.nkdx.ws.annotation.Log;
import com.nkdx.ws.dto.UserDto;
import com.nkdx.ws.eto.UserEto;
import com.nkdx.ws.response.ApiResponse;
import com.nkdx.ws.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *  用户相关逻辑
 * @author ZZW
 * @date 2022/3/3 3:51 下午
 */
@Controller
@RequestMapping("user")
public class UserController {

    private UserService userService;

    @Autowired
    public UserController (UserService userService){
        this.userService = userService;
    }

    /**
     *
     *   去 用户列表页面
     * @author ZZW
     * @date 2022/3/3 4:54 下午
     * @param
     * @return
     */
    @Log(module = "管理员管理",description = "去管理员信息管理")
    @RequestMapping("/toList")
    public String list(){
        return "user/user_list";
    }

    @Log(module = "管理员管理",description = "查询所有用户")
    @RequestMapping("findAllUsers")
    @ResponseBody
    public ApiResponse findAllUsers(){
        ApiResponse<Object> resp = new ApiResponse<>();
        return resp.success(userService.findAllUsers());
    }

    @Log(module = "管理员管理",description = "根据Id查询用户")
    @RequestMapping("findUserById")
    @ResponseBody
    public ApiResponse findUserById(Integer id){
        ApiResponse<Object> resp = new ApiResponse<>();
        return resp.success(userService.findUserById(id));
    }

    @Log(module = "管理员管理",description = "去修改用户页面")
    @RequestMapping("/toUserUpdate")
    public String toUserChangePassword(Model model){
        UserDto user = userService.getLoginUser();
        model.addAttribute("user",user);
        return "/user/user_update";
    }

    @Log(module = "管理员管理",description = "修改用户")
    @RequestMapping("update")
    @ResponseBody
    public ApiResponse update(UserEto eto){
        ApiResponse<Object> resp = new ApiResponse<>();
        return resp.success(userService.update(eto));
    }

    @RequestMapping("verificationName")
    @ResponseBody
    public ApiResponse verificationName(String name){
        ApiResponse<Object> resp = new ApiResponse<>();
        return resp.success(userService.verificationName(name));
    }

    @Log(module = "管理员管理",description = "新增用户")
    @RequestMapping("insert")
    @ResponseBody
    public ApiResponse insert(UserEto eto){
        ApiResponse<Object> resp = new ApiResponse<>();
        return resp.success(userService.insert(eto));
    }

}
